package com.example.demo.web.rest.controller;

import com.example.demo.client.evt.sysbasequery.QueryArticleEvt;
import com.example.demo.client.vm.sysbasequery.ArticleContentVm;
import com.example.demo.client.vm.sysbasequery.ArticleVm;
import com.example.demo.client.vm.sysbasequery.CatalogNameVm;
import com.ffcs.oss.param.out.ServiceResp;
import com.ffcs.oss.param.vm.QueryPageVm;
import com.example.demo.service.ArticleService;
import com.example.demo.web.rest.evt.IdEvt;
import com.example.demo.web.rest.evt.SaveArticleEvt;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 帮助文章管理
 * @Author zhengmf
 * @Date 2019/1/4  15:18
 */
@ApiModel(description ="帮助文章接口")
@RestController
@RequestMapping(value = "/api/article")
public class ArticleController extends BaseController {
    @Autowired
    private ArticleService articleService;

    @ApiOperation("保存文章信息")
    @PostMapping(value = "/saveArticle")
    public ServiceResp<Boolean> saveArticle(@RequestBody @Validated SaveArticleEvt evt){
        articleService.saveArticle(evt);
        return ServiceResp.getInstance(Boolean.class).success(true);
    }

    @ApiOperation("删除帮助文章信息，逻辑删除")
    @PostMapping(value = "/delArticle")
    public ServiceResp<Boolean> delArticle(@RequestBody @Validated IdEvt evt){
        CatalogNameVm vm = baseQueryClient.getCatalogNamesByArticleId(evt).getBody();
        if(vm != null && vm.getCatalogNames()!= null){
            String catalogName = vm.getCatalogNames();
            return ServiceResp.getInstance(Boolean.class).success(false,catalogName);
        }
        articleService.updateStatus(evt.getId());
        return ServiceResp.getInstance(Boolean.class).success(true);
    }

    @ApiOperation("查询文章")
    @PostMapping("/queryArticle")
    public ServiceResp<QueryPageVm<ArticleVm>> queryArticle(@RequestBody QueryArticleEvt evt){
        return baseQueryClient.queryArticle(evt);
    }

    @ApiOperation("查询文章内容详情")
    @PostMapping("/getArticleContentById")
    public ServiceResp<ArticleContentVm> getArticleContentById(@RequestBody @Validated IdEvt evt){
        return baseQueryClient.getArticleContentById(evt);
    }

}
